package whf.framework.report.export;

import java.lang.reflect.Constructor;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import whf.framework.config.ApplicationConfig;
import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.meta.entity.IMeta;
import whf.framework.report.export.util.ExportUtils;
import whf.framework.report.export.view.View;
import whf.framework.util.StringUtils;
import whf.framework.util.Utils;
import whf.framework.web.tag.TableModel;

/**
 * @author wanghaifeng
 * @create Dec 20, 2006 10:19:29 AM
 * 
 */
public class ReportBuilder {
	private static Log log = LogFactory.getLog(ReportBuilder.class);
	
	private static Map<String, Class> viewHandlerClasses = Utils.newHashMap();
	static {
		String names = ApplicationConfig.getInstance().getValue(ExportUtils.EXPORT_VIEW+"names");
		String[] views = StringUtils.split(names, ",");
		for(String viewName: views){
			String viewClassName = ApplicationConfig.getInstance().getValue(ExportUtils.EXPORT_VIEW+viewName);
			try{
				viewHandlerClasses.put(viewName, Class.forName(viewClassName));
			}catch(Exception e){
				log.error("View Handlers Initialized!", e);
			}
		}
		log.debug(viewHandlerClasses);
	}
	
	private ExportHandler exportHandler = new ExportHandler(this);

	private Parameters parameters;

	private HttpServletRequest request;

	public ReportBuilder(HttpServletRequest request){
		if(request == null) return;
		this.request = request;
		this.parameters = new Parameters(this.request);
		//
		TableModel tableModel = ExportUtils.getTableModel();
		String tableTitle = "";
		IMeta meta = tableModel.getMeta();
		if(meta != null){
			tableTitle = meta.getName();
		}
		String fileName = tableTitle;
		
		//init pdf exporter
		Export exportPdf =new Export();
		exportPdf.setFileName(fileName+".pdf");
		exportPdf.setView("pdf");
		exportPdf.setHeaderTitle(tableTitle);
		
		//init xls exporter
		Export exportXls =new Export();
		exportXls.setFileName(fileName+".xls");
		exportXls.setView("xls");
		exportXls.setHeaderTitle(tableTitle);
		//
		this.getExportHandler().addExport(exportPdf);
		this.getExportHandler().addExport(exportXls);
		//
		Export export = this.exportHandler.getCurrentExport();
		if(export != null){
			String currentViewName = this.exportHandler.getCurrentExport().getView();
			Class clazz = viewHandlerClasses.get(currentViewName);
			try{
				Constructor c = clazz.getConstructor(new Class[]{ReportBuilder.class, TableModel.class});
				View view = (View)c.newInstance(new Object[]{this, tableModel});
				view.beforeBody();
				view.doBody();
				Object exportData = view.afterBody();
				ExportUtils.setReportResult(exportData);
			}catch(Exception e){
				log.error(this, e);
			}
		}
	}

	public ExportHandler getExportHandler() {
		return exportHandler;
	}

	public Parameters getParameters() {
		return parameters;
	}

	public void setParameters(Parameters parameterHandler) {
		this.parameters = parameterHandler;
	}
	
	public static void main(String[] args) throws Exception{
		new ReportBuilder(null);
		Class pdf = viewHandlerClasses.get("pdf");
		pdf.newInstance();
	}
}
